<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备" prop="deviceId" class="input-length">
        <el-select v-model="queryParams.deviceId" placeholder="请选择设备" clearable filterable>
          <el-option
            v-for="item in allDeivces"
            :key="item.deviceId"
            :label="item.deviceName"
            :value="item.deviceId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="素材" prop="materialId" class="input-length">
        <el-select v-model="queryParams.materialId" placeholder="请选择素材" clearable filterable>
          <el-option
            v-for="item in allMaterials"
            :key="item.materialId"
            :label="item.materialName"
            :value="item.materialId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['screen:devicefile:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['screen:devicefile:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['screen:devicefile:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['screen:devicefile:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="devicefileList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="deviceFileId" />
      <el-table-column label="设备" align="center" prop="deviceName" />
      <el-table-column label="素材" align="center" prop="materialName" />
      <el-table-column label="播放时长(秒)" align="center" prop="playDuration" />
      <el-table-column label="播放次序" align="center" prop="playOrder" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['screen:devicefile:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['screen:devicefile:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备播放素材对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="devicefileRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备" prop="deviceId">
          <el-select v-model="form.deviceId" placeholder="请选择设备" clearable filterable>
          <el-option
            v-for="item in allDeivces"
            :key="item.deviceId"
            :label="item.deviceName"
            :value="item.deviceId"
          />
        </el-select>
        </el-form-item>
        <el-form-item label="素材" prop="materialId">
          <el-select v-model="form.materialId" placeholder="请选择素材" clearable filterable>
            <el-option
              v-for="item in allMaterials"
              :key="item.materialId"
              :label="item.materialName"
              :value="item.materialId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="播放时长(秒)" prop="playDuration">
          <el-input v-model="form.playDuration" placeholder="请输入播放时长(秒)，视频请填1" />
        </el-form-item>
        <el-form-item label="播放次序" prop="playOrder">
          <el-input v-model="form.playOrder" placeholder="请输入播放次序，可以是小数" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Devicefile">
import { listDevicefile, getDevicefile, delDevicefile, addDevicefile, updateDevicefile } from "@/api/screen/devicefile"
import { allDevice } from "@/api/screen/device"
import { allMaterial } from "@/api/screen/material"

const { proxy } = getCurrentInstance()

const devicefileList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")
const allDeivces=ref([])
const allMaterials=ref([])
const deviceIdNameMap=new Map()
const materialIdNameMap=new Map()

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    deviceId: null,
    materialId: null,
  },
  rules: {
    deviceId: [
      { required: true, message: "设备ID不能为空", trigger: "blur" }
    ],
    materialId: [
      { required: true, message: "素材ID不能为空", trigger: "blur" }
    ],
    playDuration: [
      { required: true, message: "播放时长(秒)不能为空", trigger: "blur" }
    ],
    playOrder: [
      { required: true, message: "播放次序不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询设备播放素材列表 */
function getList() {
  loading.value = true
  listDevicefile(queryParams.value).then(response => {
    devicefileList.value = response.rows
    //设置设备和素材名称
    devicefileList.value.forEach(item=>{
		
    if(item.deviceId!=null && deviceIdNameMap.has(item.deviceId)){
      item.deviceName=deviceIdNameMap.get(item.deviceId)
    }else{
      item.deviceName	= item.deviceId
    }
    if(item.materialId!=null && materialIdNameMap.has(item.materialId)){
      item.materialName=materialIdNameMap.get(item.materialId)
    }else{
      item.materialName=item.materialId
    }
    
  }) 
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    deviceFileId: null,
    deviceId: null,
    materialId: null,
    playDuration: null,
    playOrder: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  }
  proxy.resetForm("devicefileRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.deviceFileId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加设备播放素材"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _deviceFileId = row.deviceFileId || ids.value
  getDevicefile(_deviceFileId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改设备播放素材"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["devicefileRef"].validate(valid => {
    if (valid) {
      if (form.value.deviceFileId != null) {
        updateDevicefile(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addDevicefile(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _deviceFileIds = row.deviceFileId || ids.value
  proxy.$modal.confirm('是否确认删除设备播放素材编号为"' + _deviceFileIds + '"的数据项？').then(function() {
    return delDevicefile(_deviceFileIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('screen/devicefile/export', {
    ...queryParams.value
  }, `devicefile_${new Date().getTime()}.xlsx`)
}

/** 获取所有设备 */
function getAllDevice(){
  allDevice().then(response => {
    //console.log(response)
    allDeivces.value=response.data
    allDeivces.value.forEach(device=>{
      deviceIdNameMap.set(device.deviceId,device.deviceName)
    })
  })  
}

/** 获取所有素材 */
function getAllMaterial(){
  allMaterial().then(response => {
    //console.log(response)
    allMaterials.value=response.data
    allMaterials.value.forEach(material=>{
      materialIdNameMap.set(material.materialId,material.materialName)
    })
  })  
}

getAllDevice();
getAllMaterial();
getList()
</script>
<style>
.input-length {
  width: 276px; 
}
</style>